package fragment.tabhostFragment;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.RecommendDbEntity;

import java.util.ArrayList;
import java.util.List;

import dbdao.RecommendDBHelper;
import entity.PersonalDPEntity;
import entity.PersonalDetailsEntity;
import entity.PersonalSoundEntity;
import fragment.AbsBaseFragment;
import fragment.DownloadService;
import fragment.recommendfragments.ReCommendDetailsFragment;
import tools.ItemOnClickListenerOther;
import utils.Common;
import utils.adapter.PersonalDetailsAdapter;
import utils.servicedown.MyDownReceiver;
import volley.VolleySingleton;

/**
 * 个人中心
 * Created by dllo on 15/11/23.
 */
public class PersonalDetailsFragment extends AbsBaseFragment implements ItemOnClickListenerOther{
    private RecyclerView recyclerView;
    private PersonalDetailsAdapter adapter;
    private long userId;
    MyDownReceiver receiver;


    public PersonalDetailsFragment(long userId) {
        this.userId = userId;
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_details_personal;
    }

    @Override
    protected void initView() {
        receiver = new MyDownReceiver();
        recyclerView = findView(R.id.detailsPersonalRecyclerView);

    }
    @Override
    public void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Common.ACTION_DOWNLOAD_PROGRESS);
        filter.addAction(Common.ACTION_DOWNLOAD_SUCCESS);
        filter.addAction(Common.ACTION_DOWNLOAD_FAIL);
        getContext().registerReceiver(receiver, filter);


    }

    @Override
    public void onStop() {
        super.onStop();
        getContext().unregisterReceiver(receiver);

    }
    @Override
    protected void initData() {
        adapter = new PersonalDetailsAdapter(getContext());
        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter.setLinstener(this);
        recyclerView.setAdapter(adapter);
        initONetwork();
        initTNetwork();
        initSNetwork();

    }

    private void initONetwork() {
        String Url = "http://mobile.ximalaya.com/mobile/others/ca/homePage?toUid=" + userId + "&device=android";
        VolleySingleton.getInstance().addRequest(Url,
                PersonalDetailsEntity.class, new Response.Listener<PersonalDetailsEntity>() {
                    @Override
                    public void onResponse(PersonalDetailsEntity response) {
                        if (response != null) {
                            adapter.addDedata(response);
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }


    private void initTNetwork() {
        String Url = "http://mobile.ximalaya.com/mobile/others/ca/album/" + userId + "/1/2?device=android";
        VolleySingleton.getInstance().addRequest(Url,
                PersonalDPEntity.class, new Response.Listener<PersonalDPEntity>() {
                    @Override
                    public void onResponse(PersonalDPEntity response) {
                        if (response != null) {
                            adapter.addDpdata(response);
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

    private void initSNetwork() {
        String Url = "http://mobile.ximalaya.com/mobile/others/ca/track/" + userId + "/1/30?device=android";
        VolleySingleton.getInstance().addRequest(Url,
                PersonalSoundEntity.class, new Response.Listener<PersonalSoundEntity>() {
                    @Override
                    public void onResponse(PersonalSoundEntity response) {
                        if (response != null) {
                            List<PersonalSoundEntity.ListEntity> list = response.getList();
                            if (list != null) {
                                adapter.addSeList(list);
                                dismissDialog();
                            }
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

    @Override
    public void itemListener(Object o, int i) {


        switch (i){
            case 1:
                PersonalDetailsEntity deentity =(PersonalDetailsEntity)o;
                goBack();
            break;
            case 2://加关注的设置
                Toast.makeText(getContext(), "已关注", Toast.LENGTH_SHORT).show();
                PersonalDetailsEntity deen =(PersonalDetailsEntity)o;
                Toast.makeText(getContext(), "deen.getFollowingTags():" + deen.getNickname(), Toast.LENGTH_SHORT).show();
                List<RecommendDbEntity>datas = new ArrayList<>();

                RecommendDbEntity entity = new RecommendDbEntity();

                entity.setNickname(deen.getNickname());
                entity.setSmallLogo(deen.getMobileSmallLogo());
                entity.setPlayUrl(deen.getLocation());
                entity.setUid(deen.getTracks());
               // entity.setAlbmid(deen.getAlbums());
                entity.setTitle(deen.getPtitle());
                datas.add(entity);
                RecommendDBHelper.getInstance().saveRecommend(datas);


                break;
            case 3:
                PersonalSoundEntity.ListEntity entiti =
                        (PersonalSoundEntity.ListEntity) o;

                String path = entiti.getPlayPathAacv164();
                Log.d("PopularProgramPageFragm", path + "地址");
                String name =entiti.getAlbumTitle() + ".mp3";
                if (DownloadService.getInstance() != null
                        && DownloadService.getInstance().getFlag() != Common.Flag_Init)
                {
                    Toast.makeText(getContext(), "已经在下载", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent it = new Intent(getContext(), DownloadService.class);
                it.putExtra("flag", "start");
                it.putExtra("url", path);
                it.putExtra("fileName", name);
                getContext().startService(it);

                break;
            case 4:
                PersonalDPEntity dpentity = (PersonalDPEntity) o;
                long albumId = dpentity.getList().get(0).getAlbumId();
                goToNext(new ReCommendDetailsFragment(albumId,1));
                break;
            case 5:
                PersonalSoundEntity.ListEntity en =
                        (PersonalSoundEntity.ListEntity) o;
                long id = en.getTrackId();
                goToPlay(id);
                break;

        }


    }
}
